Skip to content

feat(M5c): the $eval builtin (parse+evaluate at runtime; wants_env hook)#16

Merged
flearc merged 6 commits into
mainfrom
feature/eval
Jun 23, 2026
Merged

feat(M5c): the $eval builtin (parse+evaluate at runtime; wants_env hook)#16
flearc merged 6 commits into
mainfrom
feature/eval

Conversation

@flearc

@flearc flearc commented Jun 23, 2026

Copy link
Copy Markdown
Owner

Summary

M5c implements $eval(expr [, context]) — parse a string as a JSONata expression and evaluate it at runtime, faithfully to jsonata-js v2.2.1 (outer-scope variable visibility, D3120/D3121 error codes).

  • wants_env hook$eval is the first builtin that needs a live environment (to evaluate a newly-parsed AST so it can see builtins, outer-scope bound variables, and custom functions). Added an opt-in hook to M.apply: a builtin with wants_env = true receives (env, input, ...args). The default dispatch path is byte-unchanged when the flag is absent.
  • $eval (functions/eval.lua) — lazy-requires the parser + evaluator (breaks the load cycle); undefined → undefined; pcall parse → D3120; focus-or-current-input selection; evaluates against the caller's live env directly (so ($x := 5; $eval('$x')) → 5); pcall evaluate → D3121.
  • D3120/D3121 error templates added (errors.lua), with separate parse-vs-evaluate pcall boundaries.

Results

  • Official jsonata suite: 1137 → 1147 / 1682 (+10, 68.0% → 68.6%), zero regressions. The function-eval group is fully green.
  • 416/416 unit tests green.

Verification

An adversarial fidelity review against a jsonata-js v2.2.1 oracle (~50 paired cases) confirmed error-code fidelity is exact (D3120/D3121 boundary + inner-error rewrap of $assert/$error into D3121 all match — this is suite-invisible, as the suite runner checks only that an error was raised, not its code). It found two in-scope divergences, both fixed in 465c605:

  • '1+1' ~> $eval crashed because env wasn't threaded into the ~>/partial-application apply paths (the "wire the hook into all apply paths" lesson from M5a) — now threaded.
  • A non-sequence array focus carried the internal cons flag and corrupted path indexing — now rebuilt clean.

Deferred/logged (pre-existing, engine-wide, not $eval-specific): $$ root context broken; 1/0inf vs null; deep-nested cons-array indexing; error-message text fidelity.

Test Plan

  • busted full unit suite — 416/416
  • scripts/run-suite.sh — 1147/1682, zero regressions
  • Adversarial fidelity review vs jsonata-js v2.2.1 oracle (~50 paired cases)

🤖 Generated with Claude Code

@flearc flearc merged commit bda9d29 into main Jun 23, 2026
1 check passed
@flearc flearc deleted the feature/eval branch June 23, 2026 17:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant